import sys

from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import datetime
import os.path


class Driver:
    driver = ""

    def __init__(self):
        options = webdriver.ChromeOptions()
        self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
        self.driver.implicitly_wait(2)

    def getScreenshot(self):
        # 创建屏幕截图
        # 图片图片名称 文件夹名 ../image/调用方法-xxxx-xx-xx/xxxx-xx-xx-xxx.png
        dirtime = datetime.datetime.now().strftime("%Y-%m-%d")
        dirfunc = sys._getframe().f_back.f_code.co_name
        dirname = "../images/" + dirfunc + "-" + dirtime
        # 判断dirname文件夹是否已经存在，若不存在则创建文件夹
        if not os.path.exists(dirname):
            os.mkdir(dirname)
        filename = dirname + "/" + datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S") + ".png"
        print(filename)
        self.driver.save_screenshot(filename)

        # 输入框外层不是单纯的input标签，是封装后的组件，不能直接用selennium中的clear清除
    def clear(self, element):
        actions = ActionChains(self.driver)
        # 选择输入框
        actions.click(element)
        # 全选并删除
        actions.key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL)
        actions.key_down(Keys.DELETE)
        # 执行操作
        actions.perform()

TestDriver = Driver()
